<?php

/**
 * This Controller manages the login and logout functionality and displays the
 * access-violation page.
 */
class AuthController extends Zend_Controller_Action
{
    
    /**
     * Initialize access control
     */
    public function init()
    {
	$this->_helper->acl->allow();
    }
    
    /**
     * Try and log in a user.
     * 
     * A successful login redirects to /index/index. Accepts POST-data.
     */
    public function loginAction() 
    {
	$form = new Form_Login;
	
	if($this->getRequest()->isPost())
	{
	    if($form->isValid($_POST))
	    {
		$mu = new Model_User;
		$mu->login($form->getValues());
		if($mu->isLoggedIn())
		{
		    return $this->_helper->redirector('index', 'index');
		}
		else
		{
		    $form->getElement('password')->addError(
			    'Falscher Benutzername und/oder Passwort');
		}
	    }
	}
	
	$this->view->form = $form;
    }
    
    /**
     * Logs out the current user.
     * 
     * Redirects to /auth/login.
     */
    public function logoutAction()
    {
	$mu = new Model_User;
	$mu->logout();
	return $this->_helper->redirector('login');
    }
    
    /**
     * Displays an access violation message.
     */
    public function accessViolationAction()
    {

    }
}
